package com.sky.service.user.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.Setmeal;
import com.sky.pojo.ShoppingCart;
import com.sky.service.user.ShoppingCartService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setCreateTime(LocalDateTime.now());


        shoppingCart.setUserId(BaseContext.getCurrentId());

        //判断当前加入的菜品是否已经存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

            if (list!=null && list.size()>0){
                //存在,数量+1
                ShoppingCart cart = list.get(0);
                cart.setNumber(cart.getNumber()+1);
                shoppingCartMapper.updateById(cart);
            }else {
                //不存在
                //判断本次添加的是菜品还是套餐
                Long dishId = shoppingCart.getDishId();
                if (dishId!=null){
                    //本次添加到购物车的是菜品
                  Dish dish= dishMapper.getDishById(dishId);

                  shoppingCart.setName(dish.getName());
                  shoppingCart.setImage(dish.getImage());
                  shoppingCart.setAmount(dish.getPrice());


                }else {
                    //菜品的id为空,说明添加的时套餐
                    Long setmealId = shoppingCart.getSetmealId();
                    Setmeal setmeal = setmealMapper.selectById(setmealId);

                    shoppingCart.setName(setmeal.getName());
                    shoppingCart.setImage(setmeal.getImage());
                    shoppingCart.setAmount(setmeal.getPrice());

                }
                shoppingCart.setCreateTime(LocalDateTime.now());
                shoppingCart.setNumber(1);
                shoppingCartMapper.insert(shoppingCart);
            }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> getList() {
        ShoppingCart shoppingCart=new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());

        return shoppingCartMapper.list(shoppingCart);
    }


    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {

        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }


}
